package listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.annotation.WebListener;

/*
    对应用域对象中的属性变化的监听器
 */
@WebListener
public class ServletContextAttributeListener implements javax.servlet.ServletContextAttributeListener {
    /*
        向应用域添加属性方法的时候执行此方法
     */
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        System.out.println("监听到了属性的添加...");

        //获取应用域对象
        ServletContext servletContext = scae.getServletContext();
        Object value = servletContext.getAttribute("username");
        System.out.println(value);
    }
    /*
        向应用域对象替换属性时执行此方法
     */
    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        System.out.println("监听到了属性的替换...");

        //获取应用域对象
        ServletContext servletContext = scae.getServletContext();
        Object value = servletContext.getAttribute("username");
        System.out.println(value);
    }
    /*
        向应用域对象移除属性时执行此方法
     */
    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        System.out.println("监听到了属性的移除...");

        //获取应用域对象
        ServletContext servletContext = scae.getServletContext();
        Object value = servletContext.getAttribute("username");
        System.out.println(value);
    }
}
